//
//  TFLLeftView.m
//  netEasy
//
//  Created by mac on 15/10/31.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "TFLLeftView.h"


@interface TFLLeftView()

@property(nonatomic , weak) UIButton * selectedBtn;
@end

@implementation TFLLeftView
/**
 *  初始化
 *
 *  @param frame
 *
 *  @return
 */
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self){
        //1.添加所有显示的按钮
        CGFloat alpha = 0.5;
        UIColor *newsColor = TFLColorWithRGBA(160, 60, 63, alpha);
        [self setupBtnWith:[UIImage imageNamed:@"sidebar_nav_news"] title:@"新闻" backGroundColor:newsColor leftViewCTRType:TFLLeftViewCTRNews];
        
        UIColor *readingColor = TFLColorWithRGBA(172, 105, 60, alpha);
        [self setupBtnWith:[UIImage imageNamed:@"sidebar_nav_reading"] title:@"订阅" backGroundColor:readingColor leftViewCTRType:TFLLeftViewCTRReading];
        
        UIColor *photoColor = TFLColorWithRGBA(70, 124, 192, alpha);
        [self setupBtnWith:[UIImage imageNamed:@"sidebar_nav_photo"] title:@"图片" backGroundColor:photoColor leftViewCTRType:TFLLeftViewCTRPicture];
        
        
        UIColor *videoColor = TFLColorWithRGBA(99, 154, 63, alpha);
        [self setupBtnWith:[UIImage imageNamed:@"sidebar_nav_video"] title:@"视频" backGroundColor:videoColor leftViewCTRType:TFLLeftViewCTRVideo];
        
        UIColor *commentColor = TFLColorWithRGBA(164, 166, 40, alpha);
        [self setupBtnWith:[UIImage imageNamed:@"sidebar_nav_comment"] title:@"跟贴" backGroundColor:commentColor leftViewCTRType:TFLLeftViewCTRComment];
        
        UIColor *radioColor = TFLColorWithRGBA(179, 60, 119, alpha);
        [self setupBtnWith:[UIImage imageNamed:@"sidebar_nav_radio"] title:@"电台" backGroundColor:radioColor leftViewCTRType:TFLLeftViewCTRRadio];
        
        
    }
    
    return self;

}
/**
 *  添加按钮到左边侧划中去
 *
 *  @param image          <#image description#>
 *  @param title          <#title description#>
 *  @param backGroudImage <#backGroudImage description#>
 */
-(void)setupBtnWith:(UIImage *) image title:(NSString *) title backGroundColor:(UIColor *)backGroudColor leftViewCTRType:(TFLLeftViewCTRType) type
{
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn setTitle:title forState:UIControlStateNormal];
    
    [btn setImage:image forState:UIControlStateNormal];
    
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    btn.tag = type;
    //根据颜色创建按钮的背景图片
    UIImage *selectedImage = [UIImage imageWithColor:backGroudColor];
    [btn setBackgroundImage:selectedImage forState:UIControlStateSelected];
    
    //设置按钮整体往左边移动
    btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    //设置按钮内部的控件的外边距离
    btn.titleEdgeInsets=UIEdgeInsetsMake(0, 20, 0, 0);
    btn.contentEdgeInsets=UIEdgeInsetsMake(0, 30, 0, 0);
    
    [self addSubview:btn];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    //1.设置不同按钮的FRAME值
    NSInteger btnCount = self.subviews.count;
    CGFloat btnX = 0;
    CGFloat btnW = self.frame.size.width;
    CGFloat btnH = self.frame.size.height/btnCount;
    for (int i = 0 ; i< btnCount; i++) {
        CGFloat btnY = i*btnH;
        UIButton *btn = self.subviews[i];
        btn.frame=CGRectMake(btnX, btnY, btnW, btnH);
    }
}

-(void)btnClick:(UIButton *) btn
{
    //1.调用代理方法
    if ([self.delegate respondsToSelector:@selector(leftView:from:to:)]) {
        
        [self.delegate leftView:self from:self.selectedBtn.tag to:btn.tag];
    }
    //2.设置被选中的按钮
    self.selectedBtn.selected = NO;
    btn.selected = YES;
    self.selectedBtn = btn;

}

@end
